<?php

namespace app\components\helpers;

class TreeHelper
{
    public static function format($arr, $parentId = 0, $key = 'parentId')
    {
        $list = [];
        foreach ($arr as $k => $v) {
            if ($v[$key] == $parentId) {
                $v['children'] = self::format($arr, $v['id']);
                $list[] = $v;
            }
        }
        return $list;
    }
}